<?php
/*
	[UCenter Home] (C) 2007-2008 Comsenz Inc.
	$Id: space_mtag.php 10777 2008-12-22 06:44:14Z liguode $
*/

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}

include_once(S_ROOT.'./site/libs/func_util.php');
include_once(S_ROOT.'./site/libs/func_model.php');
include_once(S_ROOT.'./source/function_cp.php');
@include_once(S_ROOT.'./data/data_profield.php');

$spotid=empty($_GET['spotid'])?0:intval($_GET['spotid']);

if ($_GET['op'] == 'new') {
    
    //实名认证
    ckrealname('share');

    //新用户见习
    cknewuser();

    if(empty($_SGLOBAL['supe_uid'])) {
        echo json_encode(retrieve_login_form());
        exit;
    }

    if(!submitcheck('spotsubmit'))
    {
        $profid = empty($_GET['profid'])?0:$_GET['profid'];

        ob_start();
        include_once template('site/tpl/create_spot');
       	$bgcontent = ob_get_contents();
        ob_end_clean();

        $r['html']=$bgcontent;
        echo json_encode($r);
        exit;
    }
    else
    {
        $r['err'] = $r['step'] = 0;
        $profid = empty($_GET['profid'])?0:intval($_GET['profid']);
        $profile_name =  empty($_POST['profile_name'])?'':$_POST['profile_name'];
        $general = empty($_POST['general'])?'':$_POST['general'];
        
        if(strlen($profile_name) < 2) {
            $r['err'] = "档案名不能小于1个中文字(2个英文字符)";
            echo json_encode($r);
            exit;
        }

        if(strlen($profile_name) > 50) {
            $r['err'] = "档案名不能多于25个中文字(50个英文字符)";
            echo json_encode($r);
            exit;
        }

        if(strlen($general) && (strlen($general) > 200)) {
            $r['err'] = "说明请保持在100个汉字之内";
            echo json_encode($r);
            exit;
        } elseif (strlen($general) && strlen($general) < 2) {
            $r['err'] = "说明至少填一个汉字！";
            echo json_encode($r);
            exit; 
        }

        $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('mtag')." WHERE tagname='$profile_name'");
        if($mtag = $_SGLOBAL['db']->fetch_array($query)) {
            if ($mtag['tagtype']) {
                //判断是否存在
                $tagid = $mtag['tagid'];
                
				if($profid == $tagid) {
					$r['err'] = "不能添加自己与自己的发现！";
                    echo json_encode($r);
					exit;
				}
                $arr = array();
                $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('mtag')." WHERE tagid='$profid' AND tagtype='1'");
                if(!$mtag2 = $_SGLOBAL['db']->fetch_array($query)) {
                    // showmessage('designated_election_it_does_not_exist');
                    // TODO : add the alert of wrong profile 1.
                    //
                    $r['err'] = "不存在该档案";
                    echo json_encode($r);
					exit;
                }
                $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('spot')." WHERE (tag1='$profid' AND tag2='$tagid') or (tag2='$profid' AND tag1='$tagid')");
                if(!$exists = $_SGLOBAL['db']->fetch_array($query)) {
                    // showmessage('designated_election_it_does_not_exist');
                    // TODO : add the alert of wrong profile 1.
                    //
                    $arr['spotnote'] = getstr($general, 150, 1, 1, 1, 1);

                    $arr['tag1'] = $profid;
                    $arr['tag2'] = $tagid;

                    $arr['uid'] = $_SGLOBAL['supe_uid'];
                    $arr['username'] = $_SGLOBAL['supe_username'];

                    $arr['dateline'] = $_SGLOBAL['timestamp'];
                    $arr['updatetime'] = $_SGLOBAL['timestamp'];

                    $sid = inserttable('spot', $arr, 1);

					//事件通知
					$matg1=getprof($arr['tag1']);
					$matg2=getprof($arr['tag2']);
					$fs = array();
					$fs['icon'] = 'spot';
					$fs['title_template'] = cplang('feed_spot_create');
					$fs['title_data'] = array('tagname' => "<a href=\"site.php?ac=image&type=spot&id=$sid\">$matg1[tagname] 和 $matg2[tagname]</a>");
					$fs['target_ids'] = "$sid";
					feed_add($fs['icon'], $fs['title_template'], $fs['title_data'], null, null, null, null, null, $fs['target_ids']);

                    $_SGLOBAL['db']->query("UPDATE ".tname('mtag')." SET spotcnt=spotcnt+1 WHERE tagid=".$profid);
                    $_SGLOBAL['db']->query("UPDATE ".tname('mtag')." SET spotcnt=spotcnt+1 WHERE tagid=".$tagid);

                    $r['spotid'] = $sid; 
                    echo json_encode($r);
                    exit;

                } else {
                    $r['err'] = "该发现已经存在";
                    echo json_encode($r);
                    exit;
                }
            }
        } else {
            // FIXME: not exist, then let user create a profile now
            //
            $r['err'] = $r['step'] = 1;
            ob_start();
            $cateselect = '<option value=0>   </option>\n';
            $subcatselect = "case \"0\":\n";
            $subcatselect .= "var sfOptions = new Array(";
            $subcatselect .= "\t\"\", \"0\");";
            $subcatselect .= "\t break;";
            $sfid = 0;
            if (!isset($fid)) {
                foreach ($_SGLOBAL['profield'] as $p) {
                    if (!$p['parentid'] && $p['profieldtype']) {
                        $cateselect .= "<option value=\"".$p[fieldid]."\">".$p[title]."</option>\n"; 
                        $subcatselect.= "case \"".$p[fieldid]."\":\n";
                        $subcatselect.= "var sfOptions = new Array(";
                        $subcatselect.= "\" \", \"0\",\n";
                        foreach ($_SGLOBAL['profield'] as $k) {
                            if ($k['parentid'] == $p['fieldid']) {
                                 $subcatselect .= "\"".$k['title']."\", \"".$k['fieldid']."\",\n"; 
                            }
                        }
                        $subcatselect = substr($subcatselect,0,-2) ."\t);\n";
                        $subcatselect .= "\tbreak;\n";
                    }
                }
            }
            include_once template('site/tpl/create_profile');
            $bgcontent = ob_get_contents();
            ob_end_clean();

            $r['html']=$bgcontent;
            echo json_encode($r);
     
            exit;
        }
        
        $r['err'] = 'oops';
        echo json_encode($r);
        exit;

        if (!isset($tagid))
            return;
   }

} elseif ($_GET['op'] == 'piclist') {
	$spotpics = getspot2prof($spotid);
	include template("site/tpl/spot_piclist");
	exit; 


} elseif ($_GET['op'] == 'editspotnote') {
    $spotid = empty($_GET['spotid']) ? 0 : intval($_GET['spotid']);
    $spotnote = $_POST['spotnote'];
    
    $r['err'] = 0;
    $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('spot')." WHERE spotid=".$spotid);
    if (!($spot =$_SGLOBAL['db']->fetch_array($query))) {
        $r['err'] = "oops, 不存在该spot";
        json_encode($r);
        exit;
    }
        
    if(strlen($spotnote) && (strlen($spotnote) > 150)) {
        $r['err'] = "简介请保持在75个汉字之内";
        echo json_encode($r);
        exit;
    } elseif (strlen($spotnote) < 2) {
        $r['err'] = "简介至少填一个汉字！";
        echo json_encode($r);
        exit; 
    }

    $spotnote = getstr($spotnote, 150, 1, 1, 1, 1);
    updatetable('spot', array('spotnote'=>$spotnote), array('spotid'=>$spotid));
    $r['spotnote'] = $spotnote;
    echo json_encode($r);
    exit;
} else {
    $p = empty($_GET['p'])?0:intval($_GET['p']); 
    $page = empty($_GET['page'])?1:intval($_GET['page']); 
    if(empty($_GET['goto'])) $_GET['goto'] = '';

    $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('spot')." WHERE spotid=".$spotid);
    if (!($spot =$_SGLOBAL['db']->fetch_array($query)))
        showmessage('no_spot');

    // get the spot's 2 profile image.
    $spotpics = getspot2prof($spotid);
    $pcount = getspotpiccnt($spotid);
	

    $ct = $_GET['goto'];
	if($ct) {
		if('up'==$ct) {
			$p --;
		}
		if('down'==$ct) {
			$p ++;
		}
	}
	//当前第几张
	$sequence = $p+1;
	//一共多少张
	$spot[picnum] = $pcount;
	
	// 做参数检查，看是否有超出的图片被选择
	if($pcount) {	
		$prep = 1; $nextp = 1;
		if($p <= 0) {
		  $p = 0;
		  $prep = 0;
		}
		
		if ($p >= ($pcount - 1)) {
		  $p = $pcount - 1;
		  $nextp = 0;
		}
	} else {
		$prep = 0;
		$nextp = 0;
	}

	$pic = getspotpics($spotid, $p);

    //图片存在
	if ($pic) 
    {		
        $picid = $pic['picid'];
       
        //图片地址
        $pic['pic'] = mkpicurl($pic, 0);
        $pic['size'] = formatsize($pic['size']);

        //图片的EXIF信息
        $exifs = array();
        $allowexif = function_exists('exif_read_data');
        if(isset($_GET['exif']) && $allowexif) {
            include_once(S_ROOT.'./source/function_exif.php');
            $exifs = getexif($pic['pic']);
        }

        //图片评论
        $perpage = 50;
        $start = ($page-1)*$perpage;
        
        //检查开始数
        ckstart($start, $perpage);

        $cid = empty($_GET['cid'])?0:intval($_GET['cid']);
        $csql = $cid?"cid='$cid' AND":'';
        $siteurl = getsiteurl();
        $list = array();
        $count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('comment')." WHERE $csql id='$pic[picid]' AND idtype='picid'"),0);
        if($count) {
            $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('comment')." WHERE $csql id='$pic[picid]' AND idtype='picid' ORDER BY dateline LIMIT $start,$perpage");
            while ($value = $_SGLOBAL['db']->fetch_array($query)) {
                realname_set($value['authorid'], $value['author']);
                $list[] = $value;
            }
        }

        //分页
        $multi = multi($count, $perpage, $page, "space.php?uid=$pic[uid]&do=$do&picid=$picid");

        //标题
        if(empty($album['albumname'])) $album['albumname'] = lang('default_albumname');

        //实名
        realname_get();

        //图片全路径
        $pic_url = $pic['pic'];
        if(!preg_match("/^http\:\/\/.+?/i", $pic['pic'])) {
            $pic_url = getsiteurl().$pic['pic'];
        }
        $pic_url2 = rawurlencode($pic['pic']);

        //访问统计
        if(!$space['self']) {
            inserttable('log', array('id'=>$space['uid'], 'idtype'=>'uid'));//延迟更新
        }
   }

    
   include template("site/tpl/spot_view");
   exit; 
}

?>
